package cc.vv.btpartyjob.server

import android.content.Context
import android.text.TextUtils
import cc.vv.baselibrary.util.LKLogUtil
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.util.download.ApkDownloadInfoObj
import cc.vv.baselibrary.util.download.DownloadApkUtil

/**
 * 版本更新相关Server
 * Created by chen.qi on 2018/9/26.
 */
object ApkDownloadServer {

    /**
     * 是否需要升级版本
     */
    private fun doYouNeedAnUpgrade(context: Context, apkDownloadInfoObj: ApkDownloadInfoObj?): Boolean {
        var doYou = false
        if (null != apkDownloadInfoObj) {
            if (!TextUtils.isEmpty(apkDownloadInfoObj.url) && null != apkDownloadInfoObj.version) {
                var code = 0
                try {
                    code = Integer.parseInt(apkDownloadInfoObj.version)
                    //保存的最新热更新的数据
                } catch (e: NumberFormatException) {
                    LKLogUtil.e(e.message, e)
                }
                if (code > UserInfoManageUtil.getVersionCode(context)) {
                    doYou = true
                }
            }
        }
        return doYou
    }


    /**
     * 有版本更新更新版本提示
     */
    fun versionHasBeenUpdated(context: Context, apkDownloadInfoObj: ApkDownloadInfoObj?) {
        if (doYouNeedAnUpgrade(context, apkDownloadInfoObj)) {
            val downloadApkUtil = DownloadApkUtil(context, apkDownloadInfoObj)
            downloadApkUtil.setOnButtonClick(object : DownloadApkUtil.OnButtonClick {
                override fun positiveClick() {
                    downloadApkUtil.downLoad()
                }

                override fun negativeClick() {}
            })
            downloadApkUtil.showUpdateDialog()
        }
    }
}